//
//  YiBaojiaViewController.m
//  Mahjong
//
//  Created by mc on 16/9/9.
//  Copyright © 2016年 Elliot. All rights reserved.
//

#import "YiBaojiaViewController.h"
#import "YiBaojiaTableViewCell.h"

#import "Header.h"
#import "waitDetaModel.h"
#import "OrderDetaModel.h"

#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface YiBaojiaViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSString *addressString;
    NSArray *countCellArr;
    NSArray *costCellArr;
    
    UILabel *nameLable;
    UILabel *contentLable;
    UILabel *numberlable;
    UILabel *costLable;
    UILabel *dataLable;
    UILabel *bnameLable;
    UILabel *bcontentLable;
    UILabel *alllable;
    
    UIImageView *headimgView;
    
    UIImageView *oneImageView;
    UIImageView *twoImageVIew;
    UIImageView *threeImageView;

}

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIView *timeView;
@property (nonatomic, strong) UITableView *CostTableview;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) waitDetaModel *model;
@end

@implementation YiBaojiaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.900 alpha:1.000];
    
    addressString = [NSString stringWithFormat:@"穹翊 18243944222 收货地址: 浙江省杭州市围垦区震泽办公大厦C座3201"];
    countCellArr = [[NSArray alloc] initWithObjects:@"卡扣 *2",@"弹簧 *1",@"胶片 *6", nil];
    costCellArr = [[NSArray alloc] initWithObjects:@"40",@"125",@"20", nil];
//    [self addNav];
    
    [self addCostTableviewUI];
    [self addTwoButtonUI];
    self.navigationItem.title = @"发布详情";
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_back"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
    backItem.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = backItem;
    self.model = [[waitDetaModel alloc] init];
    [self create];
}
-(void)create{
    [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=GetRepairSingle&repairid=%ld",self.num] success:^(id result) {
//        NSLog(@"%@",result);
        for (NSDictionary *dic in [result[0] objectForKey:@"date"]) {
            [_model setValuesForKeysWithDictionary:dic];
        }
        
        
        [self.CostTableview reloadData];
        
    } error:^(NSError *error) {
        
        
    }];
}


- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

// 处理导航栏
//- (UIStatusBarStyle)preferredStatusBarStyle
//{
//    return UIStatusBarStyleLightContent;
//}
-(void)addNav{
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDTH, 40)];
    _backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_backView];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"nav_but_back"] forState:UIControlStateNormal];
    backBtn.backgroundColor = [UIColor clearColor];
    backBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
    backBtn.frame = CGRectMake(20, 5, 25, 25);
    [_backView addSubview:backBtn];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 2 / 5 - 8, 0, WIDTH / 5, 40)];
    lable.backgroundColor = [UIColor whiteColor];
    lable.text = @"发布详情";
    lable.font = [UIFont systemFontOfSize:20];
    lable.textAlignment = NSTextAlignmentCenter;
    [self.backView addSubview:lable];
    
}
-(void)backBtnAction{
    [self.navigationController popViewControllerAnimated:YES];
}
// 1发布人信息
-(void)addDetailUI{
    UIView *statesView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/17 + 20)];
    statesView.backgroundColor = [UIColor whiteColor];
    [self.headerView addSubview:statesView];
    
    headimgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, HEIGHT/17, HEIGHT/17)];
    headimgView.backgroundColor = [UIColor grayColor];
    headimgView.image = [UIImage imageNamed:@""];
    headimgView.layer.cornerRadius = headimgView.frame.size.height/2;
    [statesView addSubview:headimgView];
    
    nameLable = [[UILabel alloc] initWithFrame:CGRectMake(HEIGHT/17 + 20,  10, WIDTH / 3, HEIGHT/34)];
    //    nameLable.backgroundColor = [UIColor redColor];
    nameLable.text = @"穹翊 2016-06-07";
    nameLable.font = [UIFont systemFontOfSize:15];
    //    nameLable.textAlignment = NSTextAlignmentCenter;
    [statesView addSubview:nameLable];
    
    contentLable = [[UILabel alloc] initWithFrame:CGRectMake(HEIGHT/17 + 20, HEIGHT/34 + 10, WIDTH/2, HEIGHT/34)];
    //    contentLable.backgroundColor = [UIColor redColor];
    contentLable.text = @"麻将机卡坏了, 不能洗牌了";
    contentLable.font = [UIFont systemFontOfSize:15];
    contentLable.textColor = [UIColor grayColor];
    [statesView addSubview:contentLable];
}
// 2图片信息
-(void)addImgViewUI{
    UIView *statesView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT/17 + 36, WIDTH, HEIGHT/6.9)];
    statesView.backgroundColor = [UIColor whiteColor];
    [self.headerView addSubview:statesView];
    
    oneImageView = [[UIImageView alloc] initWithFrame:CGRectMake((10+HEIGHT/6.5 - 20)*0 + 15, 10, HEIGHT/6.5-20, HEIGHT/6.5-20)];
    oneImageView.backgroundColor = [UIColor grayColor];
    oneImageView.image = [UIImage imageNamed:@""];
    [statesView addSubview:oneImageView];
    
    twoImageVIew = [[UIImageView alloc] initWithFrame:CGRectMake((10+HEIGHT/6.5 - 20)*1 + 15, 10, HEIGHT/6.5-20, HEIGHT/6.5-20)];
    twoImageVIew.backgroundColor = [UIColor grayColor];
    twoImageVIew.image = [UIImage imageNamed:@""];
    [statesView addSubview:twoImageVIew];
    
    threeImageView = [[UIImageView alloc] initWithFrame:CGRectMake((10+HEIGHT/6.5 - 20)*2 + 15, 10, HEIGHT/6.5-20, HEIGHT/6.5-20)];
    threeImageView.backgroundColor = [UIColor grayColor];
    threeImageView.image = [UIImage imageNamed:@""];
    [statesView addSubview:threeImageView];
    
//    for (NSInteger i = 0; i < 3; i++) {
//        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake((10+HEIGHT/6.9 - 20)*i + 15, 10, HEIGHT/6.9-20, HEIGHT/6.9-20)];
//        imgView.backgroundColor = [UIColor grayColor];
//        imgView.image = [UIImage imageNamed:@""];
//        [statesView addSubview:imgView];
//    }
}
// 3地址信息
-(void)addAddressUI{
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT/17 + 36 + HEIGHT/6.9 + 8, WIDTH, HEIGHT / 14)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.headerView addSubview:backView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, HEIGHT/22, HEIGHT/22);
    //    [btn addTarget:self action:@selector(sendAddressAction) forControlEvents:UIControlEventTouchUpInside];
    //    btn.backgroundColor = [UIColor redColor];
    [btn setImage:[UIImage imageNamed:@"else_icon_map"] forState:UIControlStateNormal];
//    btn.backgroundColor = [UIColor redColor];
    [backView addSubview:btn];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(HEIGHT/22, 0, WIDTH - HEIGHT /14, HEIGHT/14)];
//        lable.backgroundColor = [UIColor redColor];
    lable.text = addressString;
    lable.textColor = [UIColor grayColor];
    lable.font = [UIFont systemFontOfSize:15];
    lable.numberOfLines = 0;
    [backView addSubview:lable];
    
}
// 4订单号
-(void)addDingdanUI{
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT/17 + 46 + HEIGHT/6.9  + HEIGHT / 14, WIDTH, HEIGHT / 15)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.headerView addSubview:backView];
    
    numberlable = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, WIDTH/2, HEIGHT/30 -5)];
//        numberlable.backgroundColor = [UIColor redColor];
    numberlable.text = @"订单号: 6595425845";
    numberlable.font = [UIFont systemFontOfSize:15];
    numberlable.textColor = [UIColor grayColor];
    [backView addSubview:numberlable];
    
    dataLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 5 + HEIGHT/30, WIDTH/2, HEIGHT/30 -5)];
//        dataLable.backgroundColor = [UIColor redColor];
    dataLable.text = @"2016-06-07-14:00";
    dataLable.textColor = [UIColor grayColor];
    dataLable.font = [UIFont systemFontOfSize:15];
    [backView addSubview:dataLable];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 100, 10, 90, HEIGHT/15-20)];
//        lable.backgroundColor = [UIColor redColor];
    lable.text = @"未受理";
    lable.textColor = [UIColor grayColor];
    [backView addSubview:lable];
}
// 5接单时间
-(void)addTimeUI{
    self.timeView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT/17 + 46 + HEIGHT/6.9  + HEIGHT / 14 + HEIGHT/15, WIDTH, HEIGHT/17 + 20)];
    _timeView.backgroundColor = [UIColor whiteColor];
    [self.headerView addSubview:_timeView];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, HEIGHT/17, HEIGHT/17)];
    imgView.backgroundColor = [UIColor grayColor];
    imgView.image = [UIImage imageNamed:@""];
//    imgView.layer.cornerRadius = imgView.frame.size.height/2;
    [_timeView addSubview:imgView];
    
    bnameLable = [[UILabel alloc] initWithFrame:CGRectMake(HEIGHT/17 + 20,  10, WIDTH / 2, HEIGHT/34)];
    //    nameLable.backgroundColor = [UIColor redColor];
    bnameLable.text = @"如喜麻将用具品牌门店";
    bnameLable.font = [UIFont systemFontOfSize:17];
    bnameLable.textColor = [UIColor grayColor];
    //    nameLable.textAlignment = NSTextAlignmentCenter;
    [_timeView addSubview:bnameLable];
    
    bcontentLable = [[UILabel alloc] initWithFrame:CGRectMake(HEIGHT/17 + 20, HEIGHT/34 + 10, WIDTH/2, HEIGHT/34)];
    //    contentLable.backgroundColor = [UIColor redColor];
    bcontentLable.text = @"截单时间: 2016-06-07-14:00";
    bcontentLable.font = [UIFont systemFontOfSize:15];
    bcontentLable.textColor = [UIColor grayColor];
    [_timeView addSubview:bcontentLable];
}
// 维修费
-(void)addCostUI{
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_timeView.frame)+2, WIDTH, HEIGHT / 15)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.headerView addSubview:backView];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, HEIGHT/15 - 10, HEIGHT/15 - 10)];
    imgView.backgroundColor = [UIColor grayColor];
    imgView.image = [UIImage imageNamed:@""];
    [backView addSubview:imgView];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame)+10, 0, WIDTH/3, HEIGHT/15)];
//    lable.backgroundColor = [UIColor redColor];
    lable.text = @"维修费";
    [backView addSubview:lable];
    
    costLable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(backView.frame)-80, 0, 80, HEIGHT/15)];
//    costLabl/Users/mc/Desktop/麻将机/Mahjong 2/Mahjong/维修模块/RepairViewController.me.backgroundColor = [UIColor redColor];
    costLable.text = @"100";
    [backView addSubview:costLable];
}
//费用清单
-(void)addCostCountUI{
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(_timeView.frame)+2 + HEIGHT/15, WIDTH/3, 20)];
//    lable.backgroundColor = [UIColor redColor];
    lable.text = @"费用清单";
    lable.textColor = [UIColor grayColor];
    lable.font = [UIFont systemFontOfSize:15];
    [self.headerView addSubview:lable];
}
// 单项费用
- (void)addCostTableviewUI {
    self.CostTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - CGRectGetMaxY(_timeView.frame)+2 + HEIGHT/10 - 170) style:UITableViewStyleGrouped];
    self.CostTableview.delegate = self;
    self.CostTableview.dataSource = self;
    self.CostTableview.backgroundColor = [UIColor colorWithWhite:0.918 alpha:1.000];
    [self.CostTableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:self.CostTableview];
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.Item_list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"AboutTableviewCell";
    YiBaojiaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[YiBaojiaTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    NSMutableDictionary *dic = self.model.Item_list[indexPath.row];
    cell.labelCount.text = [dic objectForKey:@"Giftname"];
    cell.labelCost.text = [dic objectForKey:@"count"];
//    cell.labelCount.text = countCellArr[indexPath.row];
//    cell.labelCost.text = costCellArr[indexPath.row];
//    cell.backgroundColor = [UIColor redColor];
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT/16.5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return HEIGHT* 2/3;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    self.headerView = [[UIView alloc] init];
    _headerView.backgroundColor = [UIColor colorWithWhite:0.900 alpha:1.000];
    [self addDetailUI];
    [self addImgViewUI];
    [self addAddressUI];
    [self addDingdanUI];
    [self addTimeUI];
    [self addCostUI];
    [self addCostCountUI];
    
    nameLable.text = self.model.name;
    contentLable.text = self.model.contents;
    numberlable.text = self.model.OrderId;
    dataLable.text = self.model.addtime;
    bnameLable.text = self.model.shopname;
    bcontentLable.text = self.model.addtime;
    costLable.text = self.model.RepairMoney;
    alllable.text = self.model.Allmoney;
    [headimgView sd_setImageWithURL:[NSURL URLWithString:[_model.photourl stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
    if (_model.imagelist.count == 1) {
        [oneImageView sd_setImageWithURL:[NSURL URLWithString:[[_model.imagelist[0] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
        twoImageVIew.alpha = 0;
        threeImageView.alpha = 0;
    }
    if (_model.imagelist.count == 2) {
        [oneImageView sd_setImageWithURL:[NSURL URLWithString:[[_model.imagelist[0] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
        [twoImageVIew sd_setImageWithURL:[NSURL URLWithString:[[_model.imagelist[1] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
        threeImageView.alpha = 0;
    }
    if (_model.imagelist.count == 3) {
        //        NSString *s = [model.imagelist[0] objectForKey:@"imageurl"];
        //        NSString *newImgStr =  [[model.imagelist[0] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com"];
        [oneImageView sd_setImageWithURL:[NSURL URLWithString:[[_model.imagelist[0] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
        [twoImageVIew sd_setImageWithURL:[NSURL URLWithString:[[_model.imagelist[1] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
        [threeImageView sd_setImageWithURL:[NSURL URLWithString:[[_model.imagelist[2] objectForKey:@"imageurl"] stringByReplacingOccurrencesOfString:@"~" withString:@"http://api03.m0571.com" ]]];
        
    }

    
    return _headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return HEIGHT/16.5;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerview = [[UIView alloc] init];
    footerview.backgroundColor = [UIColor whiteColor];
    
    alllable = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 100, 0, 100, HEIGHT/16.5)];
//    lable.backgroundColor = [UIColor redColor];
    alllable.text = @"总计: 300";
    alllable.textColor = [UIColor redColor];
    [footerview addSubview:alllable];
    return footerview;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",indexPath);
}


// 取消订单 催单
-(void)addTwoButtonUI{
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    if (1 == self.mark) {
        [arr addObjectsFromArray:@[@"确定下单",@"取消下单"]];
    }else if (2 == self.mark){
        [arr addObjectsFromArray:@[@"联系门店",@"确认完成"]];
    }else{
        [arr addObjectsFromArray:@[@"联系门店",@"去评价"]];
    }
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - 110, WIDTH, 50)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    UIButton *cuiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cuiBtn.frame = CGRectMake(WIDTH - 15 - WIDTH/5, 5, WIDTH/5, 40);
    [cuiBtn addTarget:self action:@selector(cuidanAction) forControlEvents:UIControlEventTouchUpInside];
    cuiBtn.backgroundColor = [UIColor colorWithRed:255/255.0 green:61/255.0 blue:87/255.0 alpha:1.000];
    [cuiBtn setTitle:arr[0] forState:UIControlStateNormal];
    [cuiBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cuiBtn.layer.cornerRadius = 5;
    [backView addSubview:cuiBtn];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(WIDTH - 25 - WIDTH /5 - WIDTH/4, 5, WIDTH/4, 40);
    [btn addTarget:self action:@selector(cuidanAction) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor colorWithRed:255/255.0 green:178/255.0 blue:115/255.0 alpha:1.000];
    [btn setTitle:arr[1] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.layer.cornerRadius = 5;
    [backView addSubview:btn];
    
}
-(void)cuidanAction{
    NSLog(@"cuidanAction");
}

//-(void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:YES];
//    self.navigationController.navigationBarHidden = YES;
//    self.tabBarController.tabBar.hidden = YES;
//}
//-(void)viewWillDisappear:(BOOL)animated{
//    [super viewWillDisappear:YES];
//    self.navigationController.navigationBarHidden = NO;
//    self.tabBarController.tabBar.hidden = NO;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
